package cn.aitstone.service.controller;

import cn.aitstone.basic.exception.BusinessException;
import cn.aitstone.basic.utils.AjaxResult;
import cn.aitstone.basic.utils.LoginContext;
import cn.aitstone.basic.utils.PageList;
import cn.aitstone.org.domain.Employee;
import cn.aitstone.org.service.IEmployeeService;
import cn.aitstone.pet.domain.SearchMasterMsg;
import cn.aitstone.pet.dto.HandleMsgDto;
import cn.aitstone.pet.query.SearchMasterMsgQuery;
import cn.aitstone.service.domain.UserProduct;
import cn.aitstone.service.dto.HandleProductDto;
import cn.aitstone.service.query.UserProductQuery;
import cn.aitstone.service.service.IUserProductService;
import cn.aitstone.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/userProduct")
public class UserProductController {
    @Autowired
    public IUserProductService userProductService;

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 保存和修改公用的
     * @param userProduct  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody UserProduct userProduct){
        try {
            if( userProduct.getId()!=null)
                userProductService.update(userProduct);
            else
                userProductService.save(userProduct);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            userProductService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public UserProduct get(@PathVariable("id")Long id)
    {
        return userProductService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<UserProduct> list(){

        return userProductService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<UserProduct> json(@RequestBody UserProductQuery query)
    {
        return userProductService.queryData(query);
    }

    @PostMapping("/toHandle")
    public PageList<UserProduct> tohandle(@RequestBody UserProductQuery userProductQuery, HttpServletRequest request) {
        // 1.设置寻主消息状态为待审核，设置店铺Id
        userProductQuery.setState(1);
        Logininfo loginInfo = LoginContext.getLoginInfo(request);
        Employee employee = employeeService.loadByLogininfoId(loginInfo.getId());
        userProductQuery.setShopId(employee.getShopId());
        // 2.查询待审核的寻主消息寻主消息
        return  userProductService.queryData(userProductQuery);
    }

    /**
    * @Title: accept
    * @Description: 接单
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/21 15:02
    * @Parameters: [handleMsgDto]
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody HandleProductDto handleProductDto){
        try {
            userProductService.accept(handleProductDto);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }
    }
}
